<?php

namespace App\Services\Ocean;

use App\Common\Enums\NoticeStatusEnum;
use App\Common\Helpers\Functions;
use App\Common\Services\SystemApi\NoticeApiService;
use App\Enums\Ocean\OceanPromotionStatusEnum;
use App\Models\Ocean\OceanAccountModel;
use App\Models\Ocean\OceanProjectModel;
use App\Models\Ocean\OceanPromotionLogModel;

class OceanPromotionLogService extends OceanService
{
    /**
     * constructor.
     * @param string $appId
     */
    public function __construct($appId = ''){
        parent::__construct($appId);
    }

    /**
     * @param $item
     * @return bool
     * 创建
     */
    public function create($item){
        $oceanPromotionLogModel = new OceanPromotionLogModel();
        $oceanPromotionLogModel->account_id = $item['account_id'];
        $oceanPromotionLogModel->project_id = $item['project_id'];
        $oceanPromotionLogModel->promotion_id = $item['promotion_id'];
        $oceanPromotionLogModel->before_status = $item['before_status'];
        $oceanPromotionLogModel->after_status = $item['after_status'];
        $oceanPromotionLogModel->notice_status = NoticeStatusEnum::WAITING;
        return $oceanPromotionLogModel->save();
    }

    /**
     * @throws \App\Common\Tools\CustomException
     * 通知
     */
    public function notice(){
        $timeRange = [
            date('Y-m-d H:i:s', time() - 86400),
            date('Y-m-d H:i:s', time() - 180),
        ];
        $oceanPromotionLogModel = new OceanPromotionLogModel();
        $oceanPromotionLogs = $oceanPromotionLogModel->where('notice_status', NoticeStatusEnum::WAITING)
            ->whereBetween('created_at', $timeRange)
            ->get();

        $group = [];
        foreach($oceanPromotionLogs as $oceanPromotionLog){
            // 通知广告状态
            $noticePromotionStatus = [
                OceanPromotionStatusEnum::DELETED,
                OceanPromotionStatusEnum::PROJECT_OFFLINE_BUDGET,
                OceanPromotionStatusEnum::PROJECT_PREOFFLINE_BUDGET,
                OceanPromotionStatusEnum::TIME_DONE,
                OceanPromotionStatusEnum::FROZEN,
                OceanPromotionStatusEnum::AUDIT_DENY,
                OceanPromotionStatusEnum::OFFLINE_BUDGET,
                OceanPromotionStatusEnum::OFFLINE_BALANCE,
                OceanPromotionStatusEnum::PREOFFLINE_BUDGET,
                OceanPromotionStatusEnum::PROJECT_DISABLED,
                OceanPromotionStatusEnum::DISABLE_BY_QUOTA,
                OceanPromotionStatusEnum::ADVERTISER_OFFLINE_BUDGET,
                OceanPromotionStatusEnum::ADVERTISER_PREOFFLINE_BUDGET,
            ];

            if(in_array($oceanPromotionLog->after_status, $noticePromotionStatus)){
                $key = $oceanPromotionLog->project_id .'|###|'. $oceanPromotionLog->after_status;
                $group[$key][] = $oceanPromotionLog;
                $oceanPromotionLog->notice_status = NoticeStatusEnum::SUCCESS;
            }else{
                $oceanPromotionLog->notice_status = NoticeStatusEnum::DONT;
            }

            $oceanPromotionLog->save();
        }

        foreach($group as $key => $item){
            list($projectId, $status) = explode("|###|", $key);
            $statusName = Functions::getEnumMapName(OceanPromotionStatusEnum::class, $status);
            $project = OceanProjectModel::find($projectId);
            $count = count($item);

            if(empty($project)){
                continue;
            }

            $oceanAccountModel = new OceanAccountModel();
            $account = $oceanAccountModel->where('account_id', $project->account_id)->first();

            if(empty($account) || empty($account->admin_id)){
                continue;
            }

            $title = "巨量". $statusName;
            $content = implode("<br>", [
                "账户id: {$account->account_id}",
                "账户名称: {$account->name}",
                "项目id: {$projectId}",
                "项目名称: {$project->name}",
                "影响广告数: {$count}",
            ]);

            $adminId = $account->admin_id;

            if(!Functions::isLocal()){
                $noticeApiService = new NoticeApiService();
                $noticeApiService->apiSendFeishuMessage($title, $content, $adminId);
            }
        }

        return true;
    }
}
